﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using ATEC.ATDataCoreNET;
using ATEC.ATDataCoreNET.ATObjects;
using ATEC.GeometryNET;
using SSDPNET;
using SSDPNET.DataCore;
namespace ViewExample
{
    public partial class ATPreviewExampleForm : Form
    {
        public ATPreviewExampleForm()
        {
            InitializeComponent();
        }

        public static ATScene CreateOrGetScene(string sceneName)
        {
            ATProject activeProj = ATEC.ATCore.Session.Entity.GetActiveATProject();
            var sceneID = activeProj.FindSceneIdByName(sceneName);
            StatusInt status = StatusInt.Error;
            ATScene pExist = activeProj.LoadRootSceneById(ref status, sceneID);
            if (pExist != null)
            {
                return pExist;
            }

            ATScenesType sceneT = ATScenesType.Normal;
            ATScenesStatus statusCreate;
            ATScene pCanvas = activeProj.CreateNewScene(out statusCreate, sceneName, sceneT, true, null);
            return pCanvas;
        }

        static void Draw(ATScene scene)
        {
            // 绘制预览图内容，与普通绘制一样
            XPoint3d center = XPoint3d.Zero;
            double radius = 100;
            XVector3d normal = XVector3d.UnitZ;
            XEllipse3d arc = XEllipse3d.FromCenterRadiusNormal(center, radius, normal);
            CurveSet arcCurve = CurveSet.CreateDisk(arc, CurveSet.BoundaryType.Outer);

            // 添加到场景
            ATObject curveObj = GeometryToATObject.ToObject(scene, arcCurve, null);
            curveObj.AddToScene();
        }


        private void 预览1_Click(object sender, EventArgs e)
        {
            // 准备预览图使用的“画布”，场景
            ATScene pNewScene = CreateOrGetScene("x_preview_1");

            // 实始化预览控件
            SSDPObject.ATPreviewHost previewHost = new SSDPObject.ATPreviewHost(pNewScene);
            elementHost1.Child = previewHost; // 绑定预览控件
            this.Closed += (a, b) => { previewHost.Free(); }; // 善后

            // 示例：取出 view 的 Idx，对view进行操作，如关闭辅助坐标系。
            {
                int nIdx = previewHost.GetViewIndex();
                SSDPNET.IndexedViewSet viewSet = SSDPNET.IViewManager.GetActiveViewSet();
                ATEC.ATDataCoreNET.IndexedViewport vp = viewSet.GetViewport(nIdx);
                var viewF = vp.GetViewInformation().ViewFlags;
                viewF.AuxDisplay = false;
                vp.GetViewInformation().SetViewFlags(viewF);
                vp.SynchWithViewInformation(true, true);
            }

            绘制1.Click += (sender_1, e_1) =>
            {
                Draw(pNewScene); // 绘制
                previewHost.Update(); // 更新
            };
        }

        private void 预览2_Click(object sender, EventArgs e)
        {
            // 准备预览图使用的“画布”，场景
            ATScene pNewScene = CreateOrGetScene("x_preview_2");
 
            // 实始化预览控件
            SSDPObject.ATPreviewHost previewHost = new SSDPObject.ATPreviewHost(pNewScene);           
            elementHost2.Child = previewHost; // 绑定预览控件
            this.Closed += (a, b) => { previewHost.Free(); }; // 善后

            绘制2.Click += (sender_1, e_1) =>
            {
                Draw(pNewScene); // 绘制
                previewHost.Update(); // 更新
            };
        }

        private void 预览3_Click(object sender, EventArgs e)
        {
            // 准备预览图使用的“画布”，场景
            ATScene pNewScene = CreateOrGetScene("x_preview_3");

            // 实始化预览控件
            SSDPObject.ATPreviewHost previewHost = new SSDPObject.ATPreviewHost(pNewScene);
            elementHost3.Child = previewHost; // 绑定预览控件
            this.Closed += (a, b) => { previewHost.Free(); }; // 善后

            绘制3.Click += (sender_1, e_1) =>
            {
                Draw(pNewScene); // 绘制
                previewHost.Update(); // 更新
            };
        }

        private void 预览4_Click(object sender, EventArgs e)
        {
            // 准备预览图使用的“画布”，场景
            ATScene pNewScene = CreateOrGetScene("x_preview_4");

            // 实始化预览控件
            SSDPObject.ATPreviewHost previewHost = new SSDPObject.ATPreviewHost(pNewScene);
            elementHost4.Child = previewHost; // 绑定预览控件
            this.Closed += (a, b) => { previewHost.Free(); }; // 善后

            绘制4.Click += (sender_1, e_1) =>
            {
                Draw(pNewScene); // 绘制
                previewHost.Update(); // 更新
            };
        }
    }
}
